package com.itheima.api.sms;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.sms.SmsCouponHistoryMapper;
import com.itheima.vo.domain.TableDataInfo;
import com.itheima.vo.pojo.SmsCouponHistory;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

@Service
public class SmsCouponHistoryServiceImpl extends ServiceImpl<SmsCouponHistoryMapper, SmsCouponHistory> implements SmsCouponHistoryService {

    @Autowired
    private SmsCouponHistoryMapper smsCouponHistoryMapper;

    //根据优惠券id，使用状态，订单编号分页获取领取记录
    @Override
    public TableDataInfo page(Long couponId, Integer pageNum, Integer pageSize, Long useStatus, String orderSn) {
        Page<SmsCouponHistory> pageInfo = new Page<>(pageNum,pageSize);
        TableDataInfo tableDataInfo = new TableDataInfo();

        LambdaQueryWrapper<SmsCouponHistory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(couponId!=null,SmsCouponHistory::getCouponId,couponId);
        queryWrapper.eq(useStatus!=null,SmsCouponHistory::getUseStatus,useStatus);
        queryWrapper.like(StringUtils.isNotEmpty(orderSn),SmsCouponHistory::getOrderSn,orderSn);

        smsCouponHistoryMapper.selectPage(pageInfo,queryWrapper);


        tableDataInfo.setList(pageInfo.getRecords());
        tableDataInfo.setPageNum(pageNum);
        tableDataInfo.setPageSize(pageSize);
        tableDataInfo.setTotal(pageInfo.getTotal());
        tableDataInfo.setTotalPage(pageInfo.getPages());

        return tableDataInfo;
    }
}
